import { NodeSize } from '../config'

export default function joinRows(flow) {
  flow.register('JoinRows', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M743.66781 780.092952H532.72381v-80.310857H76.068571v180.175238h456.655239v-80.335238h230.473142v-134.777905h-19.529142v115.248762zM491.27619 838.534095H117.49181v-97.304381H491.27619v97.304381z'
            }
          ),
          h(
            'path',
            {
              fill: '#FFFFFF',
              d: 'M117.49181 741.205333H491.27619v97.304381H117.49181zM117.49181 204.361143H491.27619v97.328762H117.49181z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M532.72381 262.777905h210.944v107.641905h19.529142V243.248762H532.72381V162.937905H76.068571v180.199619h456.655239v-80.359619z m-41.44762 38.912H117.49181V204.361143H491.27619v97.328762z'
            }
          ),
          h(
            'path',
            {
              fill: '#FF9C04',
              d: 'M910.823619 430.421333l-9.142857-9.142857a99.718095 99.718095 0 0 0-137.97181-3.291428l-0.219428-0.195048-50.054095 50.078476a100.425143 100.425143 0 0 0-20.772572 106.764191l34.450286-34.474667c-0.048762-0.999619-0.292571-1.974857-0.292572-2.974476 0-15.750095 6.144-30.573714 17.261715-41.71581l16.359619-16.359619v0.024381l30.841905-30.793143 0.170666 0.170667a58.514286 58.514286 0 0 1 80.920381 2.023619l9.142857 9.142857a59.099429 59.099429 0 0 1 0 83.407238l-44.909714 44.909715c-16.920381 16.847238-43.300571 20.870095-64.78019 12.166095l-30.744381 30.72a99.59619 99.59619 0 0 0 53.833142 15.823238c26.819048 0 52.028952-10.459429 70.972953-29.42781l44.909714-44.909714a100.376381 100.376381 0 0 0 0.024381-141.945905z'
            }
          ),
          h(
            'path',
            {
              fill: '#FF9C04',
              d: 'M655.043048 450.56a58.660571 58.660571 0 0 1 65.048381-12.385524l30.47619-30.47619a99.474286 99.474286 0 0 0-53.833143-15.847619c-26.819048 0-52.028952 10.459429-70.972952 29.427809l-44.909714 44.909714a100.473905 100.473905 0 0 0 0 141.970286l9.142857 9.142857a99.718095 99.718095 0 0 0 137.971809 3.291429l0.24381 0.195048 40.764952-40.764953 0.073143 0.170667 7.826286-7.826286a100.498286 100.498286 0 0 0 22.064762-108.690286l-34.425905 34.425905a58.514286 58.514286 0 0 1-11.410286 38.229333l0.438857 0.438858-4.217905 4.217904c-0.609524 0.682667-1.048381 1.438476-1.706666 2.072381l-16.384 16.384-0.097524-0.292571-33.621333 33.596952-0.780191 0.024381c-22.77181 17.042286-57.002667 15.579429-77.433905-4.803047l-9.142857-9.142858a58.660571 58.660571 0 0 1-17.261714-41.691428c0-15.750095 6.144-30.573714 17.261714-41.71581l44.885334-44.860952z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '记录关联（笛卡尔输出）'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
